// ---------------------------------
// projects/collatz/TestCollatz.java
// Copyright (C) 2010
// Glenn P. Downing
// ---------------------------------

/*
To test the program:
    % setenv CLASSPATH .:/lusr/share/lib/java/junit/junit-4.5.jar
    % javac -Xlint TestCollatz.java
    % java  -ea    TestCollatz
*/

import java.io.PrintStream;
import java.util.Scanner;

import junit.framework.Assert;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

// ------
// Reader
// ------

final class Reader {
    private int _c;
    private int _i;
    private int _j;

    public Reader (int i, int j) {
        _c = 0;
        _i = i;
        _j = j;}

    public boolean hasNextInt () {
        return (_c < 2);}

    public int nextInt () {
        switch (_c) {
            case 0:
                ++_c;
                return _i;
            case 1:
                ++_c;
                return _j;
            default:
                assert false;}
        return -1;}}

// ------
// Writer
// ------

final class Writer {
    private StringBuilder _sb;

    public Writer () {
        _sb = new StringBuilder();}

    public String toString () {
        return _sb.toString();}

    public void write (String s) {
        _sb.append(s + "\n");}}

// -----------
// TestCollatz
// -----------

public final class TestCollatz extends TestCase {
    // ----
    // read
    // ----

    public void testRead () {
        final Reader  r   = new Reader(1, 10);
        final int     a[] = new int[2];
        final boolean b   = Collatz.read(r, a);
    	Assert.assertTrue(b    == true);
    	Assert.assertTrue(a[0] ==  1);
    	Assert.assertTrue(a[1] == 10);}

	//
	// cycLen 
	//

	public void testcycLen1() {
		final int v = 5;
		int cycle = Collatz.cycLen(v);
		Assert.assertTrue(cycle == 6);
	}

	public void testcycLen2() {
		final int v = 10;
		int cycle = Collatz.cycLen(v);
		Assert.assertTrue(cycle == 7);
	}

	public void testcycLen3() {
		final int v = 1;
		int cycle = Collatz.cycLen(v);
		Assert.assertTrue(cycle == 1);
	}

	//
	// cacheCycLen
	//




    // ----
    // eval
    // ----

    public void testEval1 () {
        final int v = Collatz.eval(new int[]{1, 10});
    	Assert.assertTrue(v == 20);}

    public void testEval2 () {
        final int v = Collatz.eval(new int[]{100, 200});
    	Assert.assertTrue(v == 125);}

    public void testEval3 () {
        final int v = Collatz.eval(new int[]{201, 210});
    	Assert.assertTrue(v == 89);}

    public void testEval4 () {
        final int v = Collatz.eval(new int[]{900, 1000});
    	Assert.assertTrue(v == 174);}

	public void testEval5 () {
        final int v = Collatz.eval(new int[]{1, 1000});
    	Assert.assertTrue(v == 179);}

    // -----
    // print
    // -----

    public void testPrinter () {
        final Writer w = new Writer();
        Collatz.print(w, new int[]{1, 10}, 20);
    	Assert.assertTrue(w.toString().equals("1 10 20\n"));}

    // ----
    // main
    // ----

    public static void main (String[] args) {
        System.out.println("TestCollatz.java");
        TestRunner.run(new TestSuite(TestCollatz.class));
		//Collatz.printa(Collatz.cache);
        System.out.println("Done.");}}
